<html>
<head><meta charset="utf-8"><title>overflow on debug build · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html">overflow on debug build</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198694068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694068">(May 25 2020 at 18:51)</a>:</h4>
<p>Hello! Sorry if this has been asked already, as it probably has been asked before.</p>
<p>I noticed that on debug builds shifts (<code>&gt;&gt;</code> or <code>&lt;&lt;</code>) will cause a runtime panic if you are attempting a shift that would cause an overflow (for example shifting 8 places on an <code>u8</code>), whereas on release builds that overflow is caught at compile time and the build fails, as one would expect. I suppose this is a product of a missing build phase on debug builds, but could someone please explain why this happens?</p>



<a name="198694289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694289">(May 25 2020 at 18:54)</a>:</h4>
<p>Do you mean <code>cargo check</code>?</p>



<a name="198694453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694453">(May 25 2020 at 18:57)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span>  Nope, <code>cargo run</code> will build and run just fine and panic on runtime. But <code>cargo run --release</code> will not compile at all.</p>



<a name="198694598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694598">(May 25 2020 at 18:59)</a>:</h4>
<p>Can you give an example? This is just a lint, so it might depend on optimizations, but I didn't think we ran any that could affect this.</p>



<a name="198694710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694710">(May 25 2020 at 19:01)</a>:</h4>
<p>The following gives an error in both debug and release mode on the playground:</p>
<div class="codehilite"><pre><span></span><code>fn main() {
    0u8 &lt;&lt; 8;
}
</code></pre></div>


<div class="codehilite"><pre><span></span><code>error: this arithmetic operation will overflow
 --&gt; src/main.rs:2:5
  |
2 |     0u8 &lt;&lt; 8;
  |     ^^^^^^^^ attempt to shift left with overflow
  |
  = note: `#[deny(arithmetic_overflow)]` on by default
</code></pre></div>



<a name="198694747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694747">(May 25 2020 at 19:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/overflow.20on.20debug.20build/near/198694598">said</a>:</p>
<blockquote>
<p>Can you give an example? This is just a lint, so it might depend on optimizations, but I didn't think we ran any that could affect this.</p>
</blockquote>
<p>As of right now MIR optimizations are identical in check, debug and release mode.</p>



<a name="198694947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198694947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198694947">(May 25 2020 at 19:04)</a>:</h4>
<p>Mir optimizations are usually not run in check mode.</p>



<a name="198695263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198695263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198695263">(May 25 2020 at 19:09)</a>:</h4>
<div class="codehilite"><pre><span></span><code>fn main() {
    let a: u8 = 0x01;
    let b = a &lt;&lt; 8;
}
</code></pre></div>


<div class="codehilite"><pre><span></span><code>error: attempt to shift left with overflow
 --&gt; src/main.rs:3:13
  |
3 |     let b = a &lt;&lt; 8;
  |             ^^^^^^
  |
  = note: `#[deny(exceeding_bitshifts)]` on by default

error: aborting due to previous error

error: could not compile `overflow`.
</code></pre></div>


<p>ah right, it's a lint, didn't notice the <code>deny(exceeding_bitshifts)</code> there, my bad.. but in any case, <span class="user-mention" data-user-id="116118">@Matthew Jasper</span> 's answer makes sense. thanks <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="198695560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198695560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198695560">(May 25 2020 at 19:14)</a>:</h4>
<p>But check mode is <code>cargo check</code>, not <code>cargo build</code></p>



<a name="198695905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198695905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198695905">(May 25 2020 at 19:20)</a>:</h4>
<p>I'm not running <code>cargo check</code>, I noticed those in <code>cargo run</code>, which also outputs warnings and errors.</p>



<a name="198695957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198695957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198695957">(May 25 2020 at 19:21)</a>:</h4>
<p>more importantly, <code>cargo check</code> does not show that error at all, it only shows up on <code>cargo run --release</code>, which was what caught my attention</p>



<a name="198696276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198696276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198696276">(May 25 2020 at 19:27)</a>:</h4>
<p>...and with <code>cargo run</code>, which does <code>cargo build</code>.</p>



<a name="198696282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198696282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198696282">(May 25 2020 at 19:27)</a>:</h4>
<p>I'm just confused because you said there was a difference between a debug and a release build</p>



<a name="198696926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198696926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198696926">(May 25 2020 at 19:38)</a>:</h4>
<p>there is.. <code>cargo run</code> and <code>cargo run --release</code> have different behaviours</p>



<a name="198696943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198696943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198696943">(May 25 2020 at 19:38)</a>:</h4>
<p>So far all the examples shown emit the error in both</p>



<a name="198697289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198697289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198697289">(May 25 2020 at 19:44)</a>:</h4>
<p>the example i shared is with the <code>--release</code> flag, but if I don't use the <code>--release</code> tag, as I said, it panics at runtime with the following message:</p>
<div class="codehilite"><pre><span></span><code>thread &#39;main&#39; panicked at &#39;attempt to shift left with overflow&#39;, src/main.rs:3:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
</code></pre></div>



<a name="198697443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198697443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198697443">(May 25 2020 at 19:47)</a>:</h4>
<p>It fails to compile with <code>error: this arithmetic operation will overflow</code> in both modes on the playground. Did you silence the lint somehow?</p>



<a name="198697487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198697487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198697487">(May 25 2020 at 19:48)</a>:</h4>
<p>I think we used to have a bug which sounds very similar to this but I can't find it off hand.</p>



<a name="198697505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198697505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198697505">(May 25 2020 at 19:48)</a>:</h4>
<p><span class="user-mention" data-user-id="246514">@Martin Asquino</span> are you on the latest Rust version?</p>



<a name="198697782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198697782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198697782">(May 25 2020 at 19:52)</a>:</h4>
<p>aah right, I was on 1.42, updating to 1.43.1 fixed it, thank you <span class="user-mention" data-user-id="125250">@Wesley Wiser</span></p>



<a name="198711604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/198711604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#198711604">(May 26 2020 at 00:45)</a>:</h4>
<p>I don't think you've provided enough information. This fails at compile time with a debug build:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="k">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>My guess is that when you are doing a release build, it "tries harder" to evaluate stuff at compile time.</p>



<a name="199244489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/199244489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#199244489">(May 30 2020 at 07:50)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> they were using an outdated rustc and thus hitting an already fixed bug.</p>



<a name="199244491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/199244491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#199244491">(May 30 2020 at 07:50)</a>:</h4>
<p>right now there should be no difference in diagnostics between debug and release builds.</p>



<a name="199244494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/199244494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#199244494">(May 30 2020 at 07:51)</a>:</h4>
<p>However, there <em>are</em> some differences between "check"s and "build"s.</p>



<a name="199251698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/199251698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#199251698">(May 30 2020 at 11:14)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> hmm. When I sent my message, I could only see the very first message "Hello! Sorry if this has been asked..." and there were no replies... but I now see that my message follows a lot of back-and-forth.</p>



<a name="199251727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/overflow%20on%20debug%20build/near/199251727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/overflow.20on.20debug.20build.html#199251727">(May 30 2020 at 11:15)</a>:</h4>
<p>This is very strange, but I do run Chrome Canary, which has been acting up a bit in the last few days (e.g. large parts of the screen were covered in red and unreadable). I wonder if it's something I did, Zulip did, or Chrome did.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>